#!/bin/bash          


preliminary=$(GLOBALSTKSIZE=256MB; export GLOBALSTKSIZE; PROLOGINITSIZE=512MB; export PROLOGINITSIZE; PROLOGMAXSIZE=1024MB; export PROLOGMAXSIZE)
solver='sicstus --noinfo'
files='default.pl'
test=0
options=''
debug=0
debug1=''
debug2=''
asserts=''


helptal() {
   echo "Usage: `basename $0` options (-yfidsn)"
   echo "    -y: Uses Yap"
   echo "    -f 'file1 file2 ...': Specifies the files to process (only one file supported at the moment)"
   echo "    -i: Calls the solver with a -i option (interactive mode)"
   echo "    -d: Produces a debug trace (graphml file + html). Doesn't support single seed mode"
   echo "    -s MS: sets a timeout of MS milliseconds"
   echo "    -n: Number of solutions required"
   echo "    -h: Prints this message"
   exit $E_OPTERROR        # Exit and explain usage, if no argument(s) given.	
}


# 'getopts' processes command line arguments to script.
# The arguments are parsed as "options" (flags) and associated arguments.
 
# Try invoking this script with
# 'scriptname -mn'
# 'scriptname -oq qOption' (qOption can be some arbitrary string.)
# 'scriptname -qXXX -r'
#
# 'scriptname -qr'    - Unexpected result, takes "r" as the argument to option "q"
# 'scriptname -q -r'  - Unexpected result, same as above
#  If an option expects an argument ("flag:"), then it will grab
#  whatever is next on the command line.

NO_ARGS=0 
E_OPTERROR=65

GLOBALSTKSIZE=512MB; export GLOBALSTKSIZE;
PROLOGINITSIZE=1024MB; export PROLOGINITSIZE;
PROLOGMAXSIZE=2048MB; export PROLOGMAXSIZE;

if [ $# -eq "$NO_ARGS" ]  # Script invoked with no command-line args?
then
	helptal
fi  


while getopts "yf:tids:n:h" Option; do
  case $Option in
    h     ) helptal
    		exit;;
    y     ) solver='yap -s500000 -h400000 -t300000 '
    		preliminary='';;
    f     ) files="$OPTARG";;
    t     ) test=1;;
    i     ) options='$options -i';;
    d     ) debug1='debug_on,'
    		debug2=", debug_trace('../temp/temp_trace')"
    		debug=1;;
    s     ) asserts="$asserts assert(command_option(timeout, $OPTARG)),";;
    n     ) asserts="$asserts assert(command_option(number_of_solutions, $OPTARG)),";;
    *     ) echo "Unimplemented option chosen." 
    		exit;;   # DEFAULT
  esac
done

# Decrements the argument pointer so it points to next argument.
shift $(($OPTIND - 1))

echo Running TAL on files $files using $solver



cat ${files//,/ } > 'temp/tempmerged';


cd prolog
if (( $test == 1 )); then
	echo "run_tests." | $solver -l run.pl;
else
	echo $preliminary;
	echo "true, $asserts $debug1 run_tal('"../temp/tempmerged"', S) $debug2." | $solver -l run.pl;
	if (( $debug == 1 )); then
		echo "transform('../temp/temp_trace')." | yap -l  dbg2graphml.pl -s500000 -h400000 -t300000 ;
	fi;
	echo "EXECUTED:" "true, $asserts $debug1 run_tal('"../temp/tempmerged"', S) $debug2. | $solver -l run.pl";
fi;



exit 0
  
